package org.vuzya.motivateme.activities;

import org.vuzya.motivateme.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity implements GridView.OnItemClickListener {
	private static final int[] iconMenuItems = new int[] {
			R.drawable.dashboard_newbusiness, 
			R.drawable.dashboard_readmore, 
			R.drawable.dashboard_current, 
			R.drawable.dashboard_help, 
			R.drawable.dashboard_finished,
			R.drawable.dashboard_about 
		};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		String[] textArr = getResources().getStringArray(R.array.menu_items_text);

		GridView gv = (GridView) findViewById(R.id.main_dashboard_gridview);
		MenuAdapter adapter = new MenuAdapter(textArr, iconMenuItems);
		gv.setAdapter(adapter);
		gv.setOnItemClickListener(this);
	}

	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		Intent intent = null;
		switch(position){
			case 0:	//	New business
				intent = new Intent(this, NewBusinessActivity.class);
				break;
			case 1: //	Read more
				intent = new Intent(this, ReadMoreActivity.class);
				break;
			case 2: //	Current businesses
				intent = new Intent(this, BusinessListActivity.class);
				break;
			case 3:	//	Help
				intent = new Intent(this, HelpActivity.class);
				break;
			case 4:	//	Finished businesses
				intent = new Intent(this, FinishedListActivity.class);
				break;
			case 5:	//	About
				intent = new Intent(this, AboutActivity.class);
				break;
		}
		if(intent != null){
			startActivity(intent);
		}
	}

	public class MenuAdapter extends BaseAdapter {
		private final LayoutInflater layoutInflater;
		private String[] textArray;
		private int[] iconArray;

		public MenuAdapter(String[] textArray, int[] iconArray) {
			this.textArray = textArray;
			this.iconArray = iconArray;
			layoutInflater = (LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}

		@Override
		public int getCount() {
			return textArray.length;
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return iconArray[position];
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v;
			ImageView iv;
			TextView tv;
			if (convertView != null) {
				v = convertView;
			} else {
				v = layoutInflater.inflate(R.layout.main_menu_item, null);
			}

			iv = (ImageView) v.findViewById(R.id.main_menu_icon);
			tv = (TextView) v.findViewById(R.id.main_menu_text);
			iv.setImageResource(iconArray[position]);
			tv.setText(textArray[position]);
			return v;
		}

	}
}
